package com.wen.POIProject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * Create New Excel File
 * 
 * 		// Create a new row in the current sheet
		Row row = sheet.createRow(0);
		// Create a new cell in current row
		Cell cell = row.createCell(0);
		// Set Value to new value
		cell.setCellValue("Hello World");
 * 
 * @author wen
 * @version 1.0
 */
public class App2 {


	public static void main(String[] args) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Sample sheet");
		Map<String, Object[]> data = new LinkedHashMap<String, Object[]>();
		data.put("1", new Object[]{"Emp No.", "Name", "Salary"});
		data.put("2", new Object[]{1d, "John", 150000d});
		data.put("3", new Object[]{2d, "Sam", 80000d});
		data.put("4", new Object[]{3d, "Dean", 70000d});
		
		Set<String> keyset = data.keySet();
		int rownum = 0;
		for(String key : keyset){
			Row row = sheet.createRow(rownum++);
			Object [] objArr = data.get(key);
			int cellnum = 0;
			for(Object obj : objArr){
				Cell cell = row.createCell(cellnum++);
				if(obj instanceof Date)
					cell.setCellValue((Date)obj);
				else if(obj instanceof String)
					cell.setCellValue((String)obj);
				else if(obj instanceof Double)
					cell.setCellValue((Double)obj);
				else if(obj instanceof Boolean)
					cell.setCellValue((Boolean)obj);
			}
		}
		try {
			FileOutputStream out = new FileOutputStream(new File("/Users/wen/Documents/test file/new.xls"));
			workbook.write(out);
			out.close();
			System.out.println("Excel written successfully..");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
